package com.homestay.dao.impl;

import com.homestay.beans.Vip;
import com.homestay.dao.VipDao;
import com.homestay.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

public class VipDaoImpl implements VipDao {

    QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());

    @Override
    public int addVip(Vip vip) {
        try {
            int count = queryRunner.update("insert into tb_vip values(null,?,?,?,?,?,?,?,now(),now())",
                    vip.getName(),
                    vip.getSex(),
                    vip.getCard(),
                    vip.getPhone(),
                    vip.getV_type(),
                    vip.getStartTime(),
                    vip.getEndTime()
            );
            return count;
        } catch (SQLException e) {
            throw new RuntimeException("新增失败" + e);
        }
    }

    @Override
    public List<Vip> queryVipByCard(String card) {
        try {
            return queryRunner.query("select * from tb_vip where card=?", new BeanListHandler<>(Vip.class), card);
        } catch (Exception e) {
            throw new RuntimeException("根据Card查询会员失败" + e);
        }
    }

    @Override
    public List<Vip> queryVipByPhone(String phone) {
        try {
            return queryRunner.query("select * from tb_vip where phone=?", new BeanListHandler<>(Vip.class), phone);
        } catch (Exception e) {
            throw new RuntimeException("根据Phone查询会员失败" + e);
        }
    }

    @Override
    public List<Vip> queryVipList(String phone) {
        try {
            return queryRunner.query("select * from tb_vip where phone like ?", new BeanListHandler<>(Vip.class), "%" + phone + "%");
        } catch (Exception e) {
            throw new RuntimeException("查询会员列表失败" + e);
        }
    }

    @Override
    public int deleteVipById(String id) {
        try {
            return queryRunner.update("delete from tb_vip where id=?", id);
        } catch (Exception e) {
            throw new RuntimeException("删除会员失败" + e);
        }
    }

    @Override
    public Vip queryVipById(String id) {
        try {
            return queryRunner.query("select * from tb_vip where id=?", new BeanHandler<>(Vip.class), id);
        } catch (Exception e) {
            throw new RuntimeException("根据id查询会员失败" + e);
        }
    }

    @Override
    public int updateVip(Vip vip) {
        try {
            return queryRunner.update("update tb_vip set name=?,sex=?,card=?,phone=?,v_type=?,startTime=?,endTime=?,updatetime=NOW() where id=?;",
                    vip.getName(),
                    vip.getSex(),
                    vip.getCard(),
                    vip.getPhone(),
                    vip.getV_type(),
                    vip.getStartTime(),
                    vip.getEndTime(),
                    vip.getId()
            );
        } catch (Exception e) {
            throw new RuntimeException("更新会员失败" + e);
        }
    }


}
